﻿using System;
using System.Reflection;
using AzureStorageMapper.TableStorage.Attributes;
using NUnit.Framework;
using AzureStorageMapper.TableStorage;

namespace AzureStorageMapper.Tests.TableStorage.Attributes
{
    [TestFixture]
    public class TableModelTests
    {
        [Test]
        public void ModelTest()
        {
            Assert.IsTrue(typeof(SampleTableModel).IsSubclassOf(typeof(TableEntity)));
            
            Type target = typeof(SampleTableModel);
            Assert.AreEqual("SampleTableModel", target.Name);
            Attribute[] atts = Attribute.GetCustomAttributes(target, true);
            Assert.AreEqual(1, atts.Length);
            
            PropertyInfo[] pis = typeof(SampleTableModel).GetProperties();
            Assert.AreEqual(2, pis.Length);

            Assert.AreEqual("ColumnOne", pis[0].Name);
            Assert.AreEqual(typeof(string), pis[0].PropertyType);
            Attribute[] ca1 = Attribute.GetCustomAttributes(pis[0], true);
            Assert.AreEqual(1, ca1.Length);
            Assert.AreEqual(typeof(TableColumn), ca1[0].GetType());
            TableColumn tc1 = (TableColumn)ca1[0];
            Assert.IsNull(tc1.Name);
            Assert.IsNull(tc1.ColumnType);
            Assert.AreEqual(typeof(string), pis[0].PropertyType);

            Assert.AreEqual("ColumnTwo", pis[1].Name);
            Assert.AreEqual(typeof(int), pis[1].PropertyType);
            Attribute[] ca2 = Attribute.GetCustomAttributes(pis[1], true);
            Assert.AreEqual(1, ca2.Length);
            Assert.AreEqual(typeof(TableColumn), ca2[0].GetType());
            TableColumn tc2 = (TableColumn)ca2[0];
            Assert.AreEqual("Column2", tc2.Name);
            Assert.IsNull(tc2.ColumnType);
            Assert.AreEqual(typeof(int), pis[1].PropertyType);
       }
    }

    [TableModel(Name = "SampleTable")]
    internal class SampleTableModel : TableEntity
    {
        [TableColumn]
        public string ColumnOne { get; set; }

        [TableColumn(Name = "Column2")]
        public int ColumnTwo { get; set; }

        private long ColumnThree { get; set; }

        public SampleTableModel(string partitionKey, string rowKey)
            : base(partitionKey, rowKey)
        {
        }
    }
}
